﻿using System;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Runtime.CompilerServices;
using System.Windows.Forms;
using YArchitech.Controls;
using YArchitech.LIB;
using YArchitech.LIB.YJKControls;
using Microsoft.CSharp.RuntimeBinder;

namespace YArchitech.Temp
{
	internal partial class OptionForm : System.Windows.Forms.Form
	{
		public OptionForm()
		{
			base.Icon = Resource.YArchitechIcon;
			this.InitializeComponent();
			this.InitDesighVelocity();
		}

		public OptionForm(HYDuctSystemOption option) : this()
		{
			this.SetOption(option);
		}

		private void InitDesighVelocity()
		{
			DataTable dataTable = new DataTable();
			dataTable.Columns.Add("Type", typeof(string));
			dataTable.Columns.Add("Main", typeof(double));
			dataTable.Columns.Add("Side", typeof(double));
			dataTable.Columns.Add("SideSide", typeof(double));
			dataTable.Columns.Add("Terminal", typeof(double));
			DataRow dataRow = dataTable.NewRow();
			dataRow["Type"] = "住宅";
			dataRow["Main"] = 4.0;
			dataRow["Side"] = 3.0;
			dataRow["SideSide"] = 2.5;
			dataRow["Terminal"] = 2.0;
			dataTable.Rows.Add(dataRow);
			DataRow dataRow2 = dataTable.NewRow();
			dataRow2["Type"] = "公共建筑";
			dataRow2["Main"] = 6.0;
			dataRow2["Side"] = 4.0;
			dataRow2["SideSide"] = 3.5;
			dataRow2["Terminal"] = 2.0;
			dataTable.Rows.Add(dataRow2);
			DataRow dataRow3 = dataTable.NewRow();
			dataRow3["Type"] = "工厂";
			dataRow3["Main"] = 7.5;
			dataRow3["Side"] = 4.5;
			dataRow3["SideSide"] = 4;
			dataRow3["Terminal"] = 2.0;
			dataTable.Rows.Add(dataRow3);
			this.cbBuildType.DataSource = dataTable;
			this.cbBuildType.DisplayMember = "Type";
		}

		public bool SetOption(HYDuctSystemOption option)
		{
			this.m_option = option;
			return this.Init();
		}

		private void SetValue(YJKTextBox tb, double dValue)
		{
			string format = "G";
			tb.Text = dValue.ToString(format);
			tb.Tag = dValue;
		}

		private void SetValue(YJKTextBox tb, int nValue)
		{
			tb.Text = nValue.ToString();
			tb.Tag = nValue;
		}

		private void SetValue(YJKComboBox cmb, dynamic nValue)
		{
            //if (OptionForm.<>o__12.<>p__1 == null)
            //{
            //    OptionForm.<>o__12.<>p__1 = CallSite<Func<CallSite, object, string>>.Create(Binder.Convert(CSharpBinderFlags.None, typeof(string), typeof(OptionForm)));
            //}
            //Func<CallSite, object, string> target = OptionForm.<>o__12.<>p__1.Target;
            //CallSite <>p__ = OptionForm.<>o__12.<>p__1;
            //if (OptionForm.<>o__12.<>p__0 == null)
            //{
            //    OptionForm.<>o__12.<>p__0 = CallSite<Func<CallSite, object, object>>.Create(Binder.InvokeMember(CSharpBinderFlags.None, "ToString", null, typeof(OptionForm), new CSharpArgumentInfo[]
            //    {
            //        CSharpArgumentInfo.Create(CSharpArgumentInfoFlags.None, null)
            //    }));
            //}
			//cmb.Text = target(<>p__, OptionForm.<>o__12.<>p__0.Target(OptionForm.<>o__12.<>p__0, nValue));
		}

		protected bool Init()
		{
			this.SetValue(this.tbAtmosphere, this.m_option.AtmosphericPressure);
			this.SetValue(this.tbTemperature, this.m_option.Temperature);
			this.SetValue(this.tbRelativeHumidity, this.m_option.RelativeHumidity * 100.0);
			this.SetValue(this.tbDensity, this.m_option.Density);
			this.SetValue(this.tbViscosity, this.m_option.Viscosity * 1000000.0);
			this.SetValue(this.tbMainVelocity, this.m_option.DesignVelocity_Main);
			this.SetValue(this.tbSideVelocity, this.m_option.DesignVelocity_Side);
			this.SetValue(this.tbSideSideVelocity, this.m_option.DesignVelocity_SideSdie);
			this.SetValue(this.tbTerminalVelocity, this.m_option.DesignVelocity_Terminal);
			object obj = null;
			foreach (object obj2 in this.cbBuildType.Items)
			{
				DataRowView dataRowView = obj2 as DataRowView;
				if (dataRowView != null && (string)dataRowView["Type"] == this.m_option.DesignVelocity_Type)
				{
					obj = obj2;
				}
			}
			if (obj != null)
			{
				this.cbBuildType.SelectedItem = obj;
			}
			this.chkTerminalLoss.Checked = this.m_option.TerminalLossEnable;
			this.SetValue(this.tbTerminalLoss, this.m_option.TerminalLoss);
			this.tbTerminalLoss.Enabled = this.m_option.TerminalLossEnable;
			this.SetValue(this.tbFrictionalFix, this.m_option.FrictionFix);
			this.SetValue(this.tbShockFix, this.m_option.ShockFix);
			this.SetValue(this.tbRoughness, this.m_option.Roughness * 1000.0);
			this.chkWidthFactor.Checked = this.m_option.RectRactorEnable;
			this.tbWidthFactor.Enabled = this.m_option.RectRactorEnable;
			this.tbWidthFactorRange.Enabled = this.m_option.RectRactorEnable;
			this.tbWidthFactor.Text = this.m_option.RectFactor.ToString();
			this.tbWidthFactorRange.Text = this.m_option.RectFactorDeviation.ToString();
			this.chkMainHeight.Checked = this.m_option.MainRangeEnable;
			this.tbMainMaxHeight.Enabled = this.chkMainHeight.Checked;
			this.tbMainMinHeight.Enabled = this.chkMainHeight.Checked;
			this.SetValue(this.tbMainMaxHeight, this.m_option.MainRange.Max);
			this.SetValue(this.tbMainMinHeight, this.m_option.MainRange.Min);
			this.chkSideHeight.Checked = this.m_option.SideRangeEnable;
			this.tbSideMaxHeight.Enabled = this.chkSideHeight.Checked;
			this.tbSideMinHeight.Enabled = this.chkSideHeight.Checked;
			this.SetValue(this.tbSideMaxHeight, this.m_option.SideRange.Max);
			this.SetValue(this.tbSideMinHeight, this.m_option.SideRange.Min);
			return true;
		}

		private void OptionForm_Load(object sender, EventArgs e)
		{
			base.CenterToParent();
		}

		private void chkTerminalLoss_CheckedChanged(object sender, EventArgs e)
		{
			this.tbTerminalLoss.Enabled = this.chkTerminalLoss.Checked;
		}

		private void chkWidthFactor_CheckedChanged(object sender, EventArgs e)
		{
			bool @checked = this.chkWidthFactor.Checked;
			this.tbWidthFactor.Enabled = @checked;
			this.tbWidthFactorRange.Enabled = @checked;
		}

		private void chkMainHeight_CheckedChanged(object sender, EventArgs e)
		{
			bool @checked = this.chkMainHeight.Checked;
			this.tbMainMaxHeight.Enabled = @checked;
			this.tbMainMinHeight.Enabled = @checked;
		}

		private void chkSideHeight_CheckedChanged(object sender, EventArgs e)
		{
			bool @checked = this.chkSideHeight.Checked;
			this.tbSideMaxHeight.Enabled = @checked;
			this.tbSideMinHeight.Enabled = @checked;
		}

		private void cbBuildType_SelectedIndexChanged(object sender, EventArgs e)
		{
			DataRowView dataRowView = this.cbBuildType.SelectedItem as DataRowView;
			if (dataRowView == null)
			{
				return;
			}
			double dValue = (double)dataRowView["Main"];
			this.SetValue(this.tbMainVelocity, dValue);
			double dValue2 = (double)dataRowView["Side"];
			this.SetValue(this.tbSideVelocity, dValue2);
			double dValue3 = (double)dataRowView["SideSide"];
			this.SetValue(this.tbSideSideVelocity, dValue3);
			double dValue4 = (double)dataRowView["Terminal"];
			this.SetValue(this.tbTerminalVelocity, dValue4);
		}

		private void GetValue(YJKTextBox textBox, ref double dValue)
		{
			double num = dValue;
			if (double.TryParse(textBox.Text, out num))
			{
				dValue = num;
			}
		}

		private void GetValue(YJKTextBox textBox, ref int nValue)
		{
			int num = nValue;
			if (int.TryParse(textBox.Text, out num))
			{
				nValue = num;
			}
		}

		private void btnOK_Click(object sender, EventArgs e)
		{
			double num = this.m_option.AtmosphericPressure;
			this.GetValue(this.tbAtmosphere, ref num);
			this.m_option.AtmosphericPressure = num;
			num = this.m_option.Temperature;
			this.GetValue(this.tbTemperature, ref num);
			this.m_option.Temperature = num;
			num = this.m_option.RelativeHumidity * 100.0;
			this.GetValue(this.tbRelativeHumidity, ref num);
			this.m_option.RelativeHumidity = num / 100.0;
			num = this.m_option.Density;
			this.GetValue(this.tbDensity, ref num);
			this.m_option.Density = num;
			num = this.m_option.Viscosity * 1000000.0;
			this.GetValue(this.tbViscosity, ref num);
			this.m_option.Viscosity = num / 1000000.0;
			num = this.m_option.DesignVelocity_Main;
			this.GetValue(this.tbMainVelocity, ref num);
			this.m_option.DesignVelocity_Main = num;
			num = this.m_option.DesignVelocity_Side;
			this.GetValue(this.tbSideVelocity, ref num);
			this.m_option.DesignVelocity_Side = num;
			num = this.m_option.DesignVelocity_SideSdie;
			this.GetValue(this.tbSideSideVelocity, ref num);
			this.m_option.DesignVelocity_SideSdie = num;
			num = this.m_option.DesignVelocity_Terminal;
			this.GetValue(this.tbTerminalVelocity, ref num);
			this.m_option.DesignVelocity_Terminal = num;
			DataRowView dataRowView = this.cbBuildType.SelectedItem as DataRowView;
			if (dataRowView != null)
			{
				this.m_option.DesignVelocity_Type = Convert.ToString(dataRowView["Type"]);
			}
			this.m_option.TerminalLossEnable = this.chkTerminalLoss.Checked;
			num = this.m_option.TerminalLoss;
			this.GetValue(this.tbTerminalLoss, ref num);
			this.m_option.TerminalLoss = num;
			this.m_option.TerminalLossEnable = this.tbTerminalLoss.Enabled;
			num = this.m_option.FrictionFix;
			this.GetValue(this.tbFrictionalFix, ref num);
			this.m_option.FrictionFix = num;
			num = this.m_option.ShockFix;
			this.GetValue(this.tbShockFix, ref num);
			this.m_option.ShockFix = num;
			num = this.m_option.Roughness * 1000.0;
			this.GetValue(this.tbRoughness, ref num);
			this.m_option.Roughness = num / 1000.0;
			this.m_option.RectRactorEnable = this.chkWidthFactor.Checked;
			num = this.m_option.RectFactor;
			this.GetValue(this.tbWidthFactor, ref num);
			this.m_option.RectFactor = num;
			num = this.m_option.RectFactorDeviation;
			this.GetValue(this.tbWidthFactorRange, ref num);
			this.m_option.RectFactorDeviation = num;
			this.m_option.MainRangeEnable = this.chkMainHeight.Checked;
			double max = this.m_option.MainRange.Max;
			this.GetValue(this.tbMainMaxHeight, ref max);
			double min = this.m_option.MainRange.Min;
			this.GetValue(this.tbMainMinHeight, ref min);
			this.m_option.MainRange.Set(min, max);
			this.m_option.SideRangeEnable = this.chkSideHeight.Checked;
			max = this.m_option.SideRange.Max;
			min = this.m_option.SideRange.Min;
			this.GetValue(this.tbSideMaxHeight, ref max);
			this.GetValue(this.tbSideMinHeight, ref min);
			this.m_option.SideRange.Set(min, max);
			this.m_option.Save(Product.UserConfigFile);
		}

		private const string Type = "Type";

		private const string MainVelocity = "Main";

		private const string SideVelocity = "Side";

		private const string SideSideVelocity = "SideSide";

		private const string TerminalVelocity = "Terminal";

		private HYDuctSystemOption m_option;
	}
}
